package device

import (
	"backend/internal/common/errorx"
	"context"
	"net/http"

	"backend/internal/svc"
	"backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetLatestMessageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetLatestMessageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLatestMessageLogic {
	return &GetLatestMessageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetLatestMessageLogic) GetLatestMessage(req *types.LatestMessageReq) (resp *types.LatestMessageResp, err error) {
	client := l.svcCtx.ClientManager.GetClient(req.DeviceName)
	if client == nil {
		return nil, errorx.NewCodeError(http.StatusNotFound, "无法找到该设备")
	}
	latestMessage := client.GetLatestMessage()
	return &types.LatestMessageResp{
		Status: http.StatusOK,
		Msg:    "成功获取最新消息",
		Data:   latestMessage,
	}, nil
}
